import cgi
import webapp2
from google.appengine.api import users

class MainPage(webapp2.RequestHandler):
  def get(self):
#       user = users.get_current_user()
#       if user:
#         self.response.headers['Content-Type'] = 'text/plain'
#         self.response.out.write('Hello, ' + user.nickname())
#         self.response.write('\r\n--- Misc Info: ' + self.request.uri)
#       else:
#         self.redirect(users.create_login_url(self.request.uri))

    user = users.get_current_user()
    if user:
      message = """
        <html>
          <body>
            <form action="/sign" method="post">
              <div><textarea name="content_coucou" rows="3" cols="60"></textarea></div>
              <div><input type="submit" value="Sign Guestbook %s"></div>
            </form>
          </body>
        </html>""" %user.nickname()
      self.response.out.write(message)
    else:
      self.redirect(users.create_login_url(self.request.uri))

class Guestbook(webapp2.RequestHandler):
  def post(self):
    self.response.out.write('<html><body>You wrote:<pre>')
    # Note: cgi.escape() is used to escape HTML special characters to their 
    # character entity equivalents.
    self.response.out.write(cgi.escape(self.request.get('content_coucou')))
    self.response.out.write('</pre></body></html>')




myapp = webapp2.WSGIApplication([('/', MainPage),
                                ('/sign', Guestbook)],
                                debug=True)